package com.sj.aopEvolve;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyJDKProxy implements InvocationHandler {

    private IDao IDao;

    public MyJDKProxy(IDao IDao) {
        this.IDao = IDao;
    }

    //编写工具方法：生成代理
    public IDao createProxy() {
        IDao IDaoProxy = (IDao) Proxy.newProxyInstance(IDao.getClass().getClassLoader(), IDao.getClass().getInterfaces(), this);
        return IDaoProxy;
    }
//    @Override is not allowed when implementing interface method
//    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if ("save".equals(method.getName())) {
            System.out.println("权限校验================");
        }
        return method.invoke(IDao, args);
    }
}
